﻿using Maticsoft.DBUtility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Xiaov.SmartWeb.Infrastructure.ExtMethod;

namespace Xiaov.SmartWeb.Application.PluginSrc.StatisticalSystem
{
  public partial class InsertChild : System.Web.UI.Page
  {
    public string TableName { get; set; }
    public string Year { get; set; }
    public string Month { get; set; }
    public string DWBM { get; set; }
    public string ZBBM { get; set; }
    public string CJ { get; set; }


    protected void Page_Load(object sender, EventArgs e)
    {
      TableName = Request.Form["tn"].ToRequestString();
      Year = Request.Form["year"].ToRequestString();
      Month = Request.Form["month"].ToRequestString();
      DWBM = Request.Form["dwbm"].ToRequestString();
      ZBBM = Request.Form["zbbm"].ToRequestString();
      CJ = Request.Form["cj"].ToRequestString();
      string result = AddRow();
      Response.Clear();
      Response.Write(result);
      Response.End();
    }

    public int CreateNewHC(string selectZBBM)
    {
      //先计算插入的行次
      string sql = "select count(1) from " + TableName + " where NF='" + Year + "' and YF='" + Month + "' and DWBM='" + DWBM + "' and ZBBM like '" + selectZBBM + "-%'";
      int childcount = Maticsoft.DBUtility.DbHelperOra.GetSingle(sql).ToInt32();
      sql = "select HC from " + TableName + " where NF='" + Year + "' and YF='" + Month + "' and DWBM='" + DWBM + "' and ZBBM = '" + selectZBBM + "'";
      int HC = Maticsoft.DBUtility.DbHelperOra.GetSingle(sql).ToInt32();
      int newHC = childcount + HC + 1;
      //update
      sql = "update " + TableName + " set HC=HC+1 where HC>" + (HC + childcount) + " and NF='" + Year + "' and YF='" + Month + "' and DWBM='" + DWBM + "'";
      Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql);
      return newHC;
    }

    public string AddRow()
    {
      //当前指标编码是否末级
      bool moji = DbHelperOra.Exists("select count(1) from " + TableName + " where ZBBM='" + ZBBM + "' and SFMJ='是'");

      if (moji)
      {
        StringBuilder sql = new StringBuilder();
        StringBuilder coluInfo = new StringBuilder();
        StringBuilder valueInfo = new StringBuilder();
        sql.Append("Insert into " + TableName + " (ZBBM,NF,YF,DWBM,CJ,QYJY,XSYC,SFKBJ,HC) Values (");
        sql.Append("'" + CreateNewChildPK(ZBBM) + "',");
        sql.Append("'" + Year + "',");
        sql.Append("'" + Month + "',");
        sql.Append("'" + DWBM + "',");
        sql.Append("'" + (CJ.ToInt32() + 1) + "',");
        sql.Append("'启用','显示','是'," + CreateNewHC(ZBBM) + "");
        sql.Append(")");
        DbHelperOra.ExecuteSql(sql.ToString());
        return "操作成功！";
      }
      else
      {
        throw new HttpException("不是末级节点,不能添加！");
      }
    }

    public string CreateNewChildPK(string selectZBBM)
    {
      //Hack:编码规则:所选节点编码+000001=》理论上有99万条记录/月
      string sql = "select max(zbbm) as id from " + TableName + " where length(zbbm)=length('" + selectZBBM + "')+7 and ZBBM like '" + selectZBBM + "%' and NF='" + Year + "' and YF='" + Month + "'";
      object o = DbHelperOra.GetSingle(sql.ToString());
      if (o == null)
      {
        return selectZBBM + "-" + "000001";
      }
      else
      {
        int newid = Convert.ToInt16(o.ToString().Split('-').Last()) + 1;
        int zeroLenth = 6 - newid.ToString().Length;
        string zero = "";
        for (int i = 0; i < zeroLenth; i++)
        {
          zero += "0";
        }
        zero += newid;
        return selectZBBM + "-" + zero;
      }
    }
  }
}